package com.langll.ai.neo4j;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.langll.ai.AIChatApplication;
import com.langll.ai.neo4j.dao.ClotheRepository;
import com.langll.ai.neo4j.dao.MovieRepository;
import com.langll.ai.neo4j.dao.PersonRepository;
import com.langll.ai.neo4j.dao.RelationRepository;
import com.langll.ai.neo4j.node.Clothe;
import com.langll.ai.neo4j.node.Movie;
import com.langll.ai.neo4j.node.Person;
import com.langll.ai.neo4j.relation.Chuan;
import com.langll.ai.neo4j.relation.Relation;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.List;

/**
 * neo4j 图数据库测试
 */
@Slf4j
@SpringBootTest(classes = AIChatApplication.class)
@RunWith(SpringRunner.class)
public class MovieRepositoryTest {
    @Autowired
    private MovieRepository movieRepository;

    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private RelationRepository relationRepository;

    /**
     * //     * @see 数据库效果.png
     * 初始化数据
     */
    @Test
    public void initData() {
        Movie movie = new Movie();
        movie.setTagline("民间故事");
        movie.setTitle("白蛇传");
        movie.setReleased(2024);
        // 吴家骀>>>导演了>>> 白蛇传
        Relation relation = new Relation();
        relation.setRoles(Arrays.asList("导演", "编剧"));
        movie.setRelations(Arrays.asList(relation));
        Person person = new Person(34, "吴家骀");
        relation.setPerson(person);

        // 白蛇传的主演是法海
        Person person1 = new Person(35, "法海");
        Relation relation1 = new Relation();
        relation1.setRoles(Arrays.asList("主演"));
        relation1.setPerson(person1);
        movie.setRelationList(Arrays.asList(relation1));

        personRepository.save(person1);
        personRepository.save(person);
        movieRepository.save(movie);
        addRelationship();
    }

    void addRelationship() {
        Person person1 = new Person(18, "小青");
        personRepository.save(person1);
        Movie movie = movieRepository.findAll().get(0);
        List<Relation> relationList = movie.getRelationList();
        Relation relation1 = new Relation();
        relation1.setRoles(Arrays.asList("主演"));
        relation1.setPerson(person1);
        relationList.add(relation1);
        movieRepository.save(movie);
        addClothe();
    }


    @Autowired
    private ClotheRepository clotheRepository;

    void addClothe() {
        List<Clothe> clotheList = CollUtil.newArrayList();
        clotheList.add(new Clothe("T恤"));
        clotheList.add(new Clothe("牛仔"));
        clotheList.add(new Clothe("衬衫"));
        clotheList.add(new Clothe("帽子"));
        clotheRepository.saveAll(clotheList);

        Person person = personRepository.findAll().get(2);
        List<Chuan> chuanList = CollUtil.newArrayList();
        chuanList.add(new Chuan("阿迪", clotheRepository.findAll().get(1)));
        chuanList.add(new Chuan("安踏", clotheRepository.findAll().get(2)));
        person.setChuanList(chuanList);
        personRepository.save(person);
    }


    /**
     * 查询所有数据
     */
    @Test
    public void movieQuery() {
        List<Movie> movieList = movieRepository.findAll();
        log.info("movieList:{}", JSONUtil.toJsonPrettyStr(movieList));
    }


    /**
     * 删除所有数据
     */
    @Test
    public void deleteAll() {
        movieRepository.deleteAll();
        personRepository.deleteAll();
        relationRepository.deleteAll();
        clotheRepository.deleteAll();
    }


//    @Test
//    void 单步自定义查询() {
//        // MATCH (n:Movie)-[r:`关系`|`关系1`]-(p:Person) return n,p;
//        // MATCH (n:Movie)-[r:`关系`]-(p:Person) return n,p;
//        List<Movie> movieList = movieRepository.queryMovie();
//        log.info("movieList:{}", JSONUtil.toJsonPrettyStr(movieList.get(0)));
//    }
//
//    @Test
//    void 关系自定义查询() {
//        // MATCH ()-->() RETURN count(*);
//        Long count =  movieRepository.queryRelations();
//        log.info("count:{}", count);
//    }
}
